<!-- #section:basics/content.breadcrumbs -->
<div class="breadcrumbs" id="breadcrumbs">
    <script type="text/javascript">
        try {
            ace.settings.check('breadcrumbs', 'fixed')
        } catch (e) {
        }
    </script>

    <ul class="breadcrumb">
        <li>
            <i class="ace-icon fa fa-home home-icon"></i>
            <a href="#">爱播</a>
        </li>
        <li class="active">公会列表</li>
    </ul>
    <!-- /.breadcrumb -->


    <!-- /section:basics/content.searchbox -->
</div>

<!-- /section:basics/content.breadcrumbs -->
<div class="page-content">
    <form method="post" name="queryForm" action="./partner/getPartner" id="queryForm">
        <div class="page-header">
            <div class="row" style="vertical-align: middle">
                <div class="col-xs-4" id="nav-search">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="input-group">
                                <input type="text" name="queryString" id="queryString"
                                       class="form-control search-query" placeholder="用户名、昵称、描述...">
                                <span class="input-group-btn">
                                    <button type="button" onclick="doQuery(1)"
                                            class="btn btn-primary btn-sm">
                                        <span class="ace-icon fa fa-search icon-on-right bigger-110"></span>
                                        查询
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="#modal-category" role="button" class="btn btn-sm btn-success"
                       onclick="$('#submitForm').clearForm();validator.resetForm();" href="#modal-category" data-toggle="modal">
                        <i class="ace-icon fa fa-pencil-square-o bigger-120 "></i>添加公会
                    </a>
                </div>
            </div>
        </div>
        <!-- /.page-header -->
    </form>
    <div class="row">
        <div class="col-xs-12">
            <table class="table table-striped table-bordered table-hover">
                <thead>
                <tr>
                    <th>用户名</th>
                    <th>公会名称</th>
                    <th>联系人手机</th>
                    <th>邮箱</th>
                    <th>描述</th>
                    <th>最后登陆IP</th>
                    <th>最后登陆时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="user_list"></tbody>
            </table>
        </div>
    </div>
    <div id="biuuu_city" style="margin: 5px;text-align: center"></div>
    <!-- /.row -->
    <div id="modal-category" class="modal fade" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header no-padding">
                    <div class="table-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            <span class="white">&times;</span>
                        </button>
                        公会信息
                    </div>
                </div>
                <form class="form-horizontal" role="form" method="post" id="submitForm" action="./partner/SavePartner">
                    <input type="hidden" id="uid" name="uid" value="0">
                    <div class="page-content">
                        <div class="row">
                            <div class="col-sm-12  col-xs-12 ">
                                <div class="widget-box">
                                    <div class="widget-header widget-header-blue widget-header-flat">
                                        <div class="space-4"></div>
                                        <div class="form-group">
                                            <label class="col-sm-2 col-xs-12 control-label no-padding-right">
                                                账号 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <input type="text" id="username" name="username"
                                                       placeholder="登陆账号名..."
                                                       class="col-xs-10 col-sm-5"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 col-xs-12 control-label no-padding-right">
                                                密码 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <input type="text" id="password" name="password"
                                                       placeholder="登陆密码..."
                                                       class="col-xs-10 col-sm-5"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 col-xs-12 control-label no-padding-right">
                                                名称 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <input type="text" id="nickname" name="nickname"
                                                       placeholder="显示名称..."
                                                       class="col-xs-10 col-sm-5"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 col-xs-12 control-label no-padding-right">
                                                邮箱 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <input type="text" id="email" name="email"
                                                       placeholder="输入公会联系人邮箱..."
                                                       class="col-xs-10 col-sm-5"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 col-xs-12 control-label no-padding-right">
                                                联系方式 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <input type="text" id="phoneNumber" name="phoneNumber"
                                                       placeholder="输入公会联系人手机号..."
                                                       class="col-xs-10 col-sm-5"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2  col-xs-12  control-label no-padding-right">
                                                描述 </label>
                                            <div class="col-sm-9  col-xs-12 ">
                                                <textarea class="form-control" id="des" name="des" rows="4"
                                                    placeholder="请输入描述"></textarea>
                                                <p>&nbsp;</p>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer no-margin-top text-right">
                                        <button class="btn btn-sm btn-success " type="submit">
                                            <i class="ace-icon fa fa-check"></i>
                                            提交
                                        </button>
                                    </div>
                                </div>
                                <!-- /.col -->
                            </div>
                            <!-- /.row -->
                        </div>
                    </div>
                </form>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>
<!-- /.page-content -->
<script>
    var submitLock = false;
    var validator;
    var options = {
        beforeSerialize:function() {
            if($('#avatarFile').val() == '') {
                $('#avatarFile').remove();
            }
        },
        beforeSubmit: function () {
            if (submitLock) {
                return false;
            }
            info('正在保存，请稍等...');
            submitLock = true;
            return true;
        },
        success: function (data) {
            submitLock = false;
            if (data.resultCode == 0) {
                succ('操作成功');
                $('#modal-category').modal('toggle');
                delay(function () {
                    //$('#modal-category').modal('hide');
                    load('./partner/manager');
                }, 1000);
            } else {
                error(data.resultMsg);
            }
        },
        error: function (data) {
            submitLock = false;
            error('系统繁忙，请稍候重试');
        },
        dataType: 'json',
        type: 'POST',
        timeout: 50000
    };

    $(document).ready(function() {
        getPageData(1);
        validator = $("#submitForm").validate({
            errorElement: "em",
            errorPlacement: function (error, element) {
                element.after(error);
            },
            rules: {
                "username": {
                    required: true
                },
                "nickname": {
                    required: true
                },
            },
            messages: {
                "username": {
                    required: '请输入手机号'
                },
                "nickname": {
                    required: '请输入昵称'
                },
            },
            submitHandler: function (form) {
                if (submitLock) {
                    return;
                }
                $(form).ajaxSubmit(options);
            }
        });
    });

    function showEditDialog(uid) {
        validator.resetForm();
        $('#submitForm').clearForm();
        $('#submitForm').find('#uid').val(uid);
        $.ajax(
                {
                    type:"POST",
                    url:'./partner/queryPartner',data:{uid:uid},
                    success:function(data) {
                        $.each(data,function(i,item){
                            if(i == 'password') {
                            } else {
                                $('#submitForm').find('#' + i).val(item);
                            }
                        });},
                    dataType:'JSON'});
        $('#modal-category').modal('show');
    }

    function undefinedToBlank(value){
        if(value == null ){
            return '';
        }
        if(typeof(value) == "undefined"){
            return '';
        }else{
            return value;
        }
    }

    function doQuery() {
        getPageData(1);
    }

    function getPageData(curr){
        var brands;
        $.ajax({
            url: "./partner/getPartner",
            type: "POST",
            dataType: 'json',
            data: {
                "pgNum" : curr,
                "pgSize" : 12,
                "query" : $("#queryString").val()
            },

            success: function (data) {
                brands = data["list"];
                //调用分页
                laypage({
                    cont: 'biuuu_city',
                    pages: data['pages'],
                    curr: curr, //当前页
                    jump: function(obj, first){ //触发分页后的回调
                        if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                            getPageData(obj.curr);
                        }
                    }
                })

                var str = "";
                for(var i in brands){
                    var kys = brands[i];
                    str += "<tr>";
                    str += "<td >" + kys['username'] + "</td>";
                    str += "<td >" + kys['nickname'] + "</td>";
                    str += "<td >" + kys['phoneNumber'] + "</td>";
                    str += "<td >" + undefinedToBlank(kys['email']) + "</td>";
                    str += "<td >" + undefinedToBlank(kys['des']) + "</td>";
                    str += "<td >" + undefinedToBlank(kys['lastLoginIp']) + "</td>";
                    str += "<td >" + undefinedToBlank(kys['lastLoginTime']) + "</td>";
                    var icon =  'ace-icon glyphicon  glyphicon-ok bigger-130 ';
                    var color = "green";
                    if (kys['status'] == 1) {
                        color = "red";
                        icon ="ace-icon fa fa-ban bigger-130";
                    }
                    str += "<td >"
                            +"<a class=\""+color+"\" href=\"#\""
                            +" onclick=\"DisableUser('"+kys['uid'] +"')\""
                            +"hideFocus=\"true\">"
                            +"<i class=\""+icon+"\" hideFocus=\"true\"></i>"
                            +"</a>";
                            + "</td>";
                    str += "<td>";
                    str += "<div class=\"action-buttons\">"
                            +"<a class=\"green\" href=\"#\""
                            +" onclick=\"showEditDialog('"+kys['uid'] +"')\""
                            +"title=\"编辑\""
                            +"hideFocus=\"true\">"
                            +"<i class=\"ace-icon fa fa-pencil bigger-130\" hideFocus=\"true\"></i>"
                            +"</a>";

                    str += "<a class=\"red\" href=\"javascrip:void(0)\" onclick=\"deletePartner("+kys['uid']+")\""
                            +"title=\"删除\">"
                            +"<i class=\"ace-icon fa fa-trash-o bigger-130\"></i>"
                            +"</a>"
                            +"</div>";

                    str += "</td>";
                    str += "</tr>";
                }
                $("#user_list").html(str);
            },
            error:function(data){
                layer.msg('获取记录失败！', {icon: 5});
            }
        });
    }

    function DisableUser(Id) {
        $.ajax({
            type: "POST",
            url: './partner/disable',
            data: {parterId: Id},
            success: function (data) {
                if (data.resultCode == 0) {
                    succ('操作成功！');
                    getPageData(1);
                } else {
                    error('操作失败！');
                }
            },
            dataType: 'JSON'
        });
    }

    function deletePartner(Id) {
        confirm('确认删除?', function () {
            $.ajax({
                type: "GET",
                url: './partner/delete',
                data: {parterId: Id},
                success: function (data) {
                    if (data.resultCode == 0) {
                        succ('删除成功！');
                        getPageData(1);
                    } else {
                        error('删除失败！');
                    }
                },
                dataType: 'JSON'
            });
        });
    }
</script>